package com.gxm.base.jdk8.optional;

import jdk.nashorn.internal.runtime.options.Option;
import lombok.Data;
import org.junit.Test;

import java.util.Optional;

public class Demo {
    @Test
    public void Demo1(){
        System.out.println(1);
        Optional.ofNullable(1).ifPresent(System.out::println);
        Optional.ofNullable(null).ifPresent(System.out::println);
        Optional.of(null);
    }

    @Test
    public void Demo2(){
        User user = new User();
//        user.setAddress("濮阳");
//        user.setName("民");
        Optional.ofNullable(user).map(User::getName);
        System.out.println(Optional.ofNullable(user).map(User::getName));
        Optional optional = Optional.ofNullable(user).flatMap(user1 -> Optional.ofNullable(user1.getAddress()));
        System.out.println(optional.get());
    }
}

@Data
class User{
    private String name;
    private String address;
}